package com.xbongbong.saas.enums.business;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.ScreenTypeEnum;
import com.xbongbong.saas.enums.SetTypeEnum;
import com.xbongbong.saas.enums.ShowTypeEnum;
import com.xbongbong.saas.enums.base.ManagementBaseEnum;
import com.xbongbong.saas.enums.dictionary.PayPlanStatusEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 付款计划枚举
 *
 * @author feng.zheng
 * @version v1.0
 * @date 2019/1/14 15:37
 * @since v1.0
 *
 * 资金管理2期
 * 付款计划、关联供应商、关联采购合同顺序调换原9970、9990、9980-->9990、9980、9970
 * 删除归属人、支付方式visible原1-->0，先保留，保证上线成功后再删除
 * 新增字段 开票金额、坏账金额
 */
@Getter
public enum PayPlanEnum {
    // 付款计划模板设置界面展示字段
    // 基本信息8
    BASE_INFO("other_1", "enum.payPlan.attrName.base_info", "baseInfo", FieldTypeEnum.SEPARATOR.getType(), SetTypeEnum.DISABLE_REPEAT.getCode(), 1, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 10000, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    PAY_PLAN_NO("serialNo", "enum.payPlan.attrName.pay_plan_no", "payPlanNo", FieldTypeEnum.SERIALNO.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9990, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    LINK_SUPPLIER("text_3", "enum.payPlan.attrName.link_supplier", "linkSupplier", FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9980, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    LINK_PURCHASE("text_4", "enum.payPlan.attrName.link_purchase", "linkPurchase", FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9970, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    AMOUNT("num_1", "enum.payPlan.attrName.amount", "amount", FieldTypeEnum.NUM.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9960, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    REAL_AMOUNT("num_2", "enum.payPlan.attrName.real_amount", "realAmount", FieldTypeEnum.NUM.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9950, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    UN_AMOUNT("num_3", "enum.payPlan.attrName.un_amount", "unAmount", FieldTypeEnum.NUM.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9940, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    BAD_AMOUNT("num_31","enum.payPlan.attrName.bad_amount","badAmount",FieldTypeEnum.NUM.getType(),1,1,0,ShowTypeEnum.ALL.getCode(),9939,1,0,ScreenTypeEnum.SCREEN_GROUP.getType()),
    INVOICE_AMOUNT("num_32", "enum.payPlan.attrName.invoice_amount", "invoiceAmount", FieldTypeEnum.NUM.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9938, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    STATUS("text_2", "enum.payPlan.attrName.status", "status", FieldTypeEnum.COMBO.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9930, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    BELONGER("text_9", "enum.payPlan.attrName.belonger", "belonger", FieldTypeEnum.USER.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9929, 0, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    PAYMENT_METHOD("text_10", "enum.payPlan.attrName.payment_method", "paymentMethod", FieldTypeEnum.COMBO.getType(), 1, 0, 0, ShowTypeEnum.EDIT.getCode(), 9928, 0, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    PAY_TYPE("text_5", "enum.payPlan.attrName.pay_type", "PayType", FieldTypeEnum.COMBO.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9920, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    ESTIMATE_TIME("date_1", "enum.payPlan.attrName.estimate_time", "estimateTime", FieldTypeEnum.DATETIME.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9910, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    MEMO("text_8","enum.payPlan.attrName.memo","memo",FieldTypeEnum.TEXTAREA.getType(),3,0,0,ShowTypeEnum.ALL.getCode(),9900,1,0, ScreenTypeEnum.SCREEN_SHOW.getType()),
    // 系统信息
    SYSTEM_INFO("other_2", "enum.payPlan.attrName.system_info", "systemInfo", FieldTypeEnum.SEPARATOR.getType(), SetTypeEnum.DISABLE_REPEAT.getCode(), 1, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 9890, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    CREATOR_ID("creatorId", "enum.payPlan.attrName.creator_id", "creatorId", FieldTypeEnum.CREATORID.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9880, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    ADD_TIME("addTime", "enum.payPlan.attrName.add_time", "addTime", FieldTypeEnum.ADDTIME.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9870, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    UPDATE_TIME("updateTime", "enum.payPlan.attrName.update_time", "updateTime", FieldTypeEnum.UPDATETIME.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9860, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    // 逻辑字段
    ADD_PAYPLAN("array_1", "enum.payPlan.attrName.add_payplan", "addPayPlan", FieldTypeEnum.LINK_FORM.getType(), 1, 1, 0, ShowTypeEnum.EDIT.getCode(), 9850, 1, 1, ScreenTypeEnum.SCREEN_OFF.getType()),
    REAL_PAY_TIME("date_2", "enum.payPlan.attrName.real_pay_time", "realPayTime", FieldTypeEnum.DATETIME.getType(), 1, 1, 1, ShowTypeEnum.LIST_DETAIL.getCode(), 9840, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    LINK_SUPPLIER_LINK_TEXT("text_6", "enum.payPlan.attrName.link_supplier_link_text", "linkSupplierLinkText", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9830, 1, 1, ScreenTypeEnum.SCREEN_OFF.getType()),
    LINK_PURCHASE_LINK_TEXT("text_7", "enum.payPlan.attrName.link_purchase_link_text", "linkPurchaseLinkText", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9820, 1, 1, ScreenTypeEnum.SCREEN_OFF.getType()),
    HAS_RED("num_33","enum.payPlan.attrName.has_red", "hasRed", FieldTypeEnum.COMBO_NUM.getType(), 1, 1, 1, ShowTypeEnum.NONE.getCode(), 9819, 0, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    OWNER_ID("ownerId", "enum.payPlan.attrName.owner_id", "ownerId", FieldTypeEnum.OWNERID.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9790, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    /**
     * 协同人字段
     */
    COUSERID("coUserId", "enum.payPlan.attrName.couserid", "coUserId", FieldTypeEnum.COUSERID.getType(), 3, 1, 0, ShowTypeEnum.ALL.getCode(), 9780, 1, 0, ScreenTypeEnum.SCREEN_SEARCH.getType()),

    ;

    /**
     * 英文名称
     */
    private String attr;
    /**
     * 中文名称
     */
    private String attrName;
    /**
     * 对应saas中的attr
     */
    private String saasAttr;
    /**
     * 字段类型，详见
     *
     * @see FieldTypeEnum
     */
    private Integer fieldType;
    /**
     * 重命名、判重、启用必填等权限
     * 1.重命名+字段权限
     * 2.重命名+字段权限+查重
     * 3.重命名+字段权限+启用、必填
     * 4.重命名+字段权限+查重+启用、必填
     * 注：pro中启用和必填是同一个权限，同时启用或同时关闭
     */
    private Integer setType;
    /**
     * 模板设计阶段字段是否允许编辑
     * 1：允许编辑
     * 0：不允许编辑
     */
    private Integer designEditable;
    /**
     * 字段是否显示在模板设计界面
     * 1：不显示
     * 0：显示
     */
    private Integer editHide;
    /**
     * 字段在各个界面（新建、编辑、列表、详情）的展现形式
     * 详见
     *
     * @see ShowTypeEnum
     */
    private Integer showType;
    /**
     * 字段在模板设计界面的排序
     * 建议不要用连续的数字，可以用等差数列，以防以后在两个字段之间加入新字段
     */
    private Integer sort;
    /**
     * 字段在各个界面是否可见(启用)
     * 1：可见
     * 0：不可见
     * 注：字段在具体页面的可见性需要用属性和showType联合判断，在visible=1的情况下判断showType
     */
    private Integer visible;
    /**
     * 字段是否必填
     * 1：必填
     * 0：不必填
     */
    private Integer required;

    /**
     * 筛选类型
     * 1：可筛选
     * 2：不可筛选
     */
    private Integer screenType;

    PayPlanEnum(String attr, String attrName, String saasAttr, Integer fieldType, Integer setType, Integer designEditable,
                Integer editHide, Integer showType, Integer sort, Integer visible, Integer required, Integer screenType) {
        this.attr = attr;
        this.attrName = attrName;
        this.saasAttr = saasAttr;
        this.fieldType = fieldType;
        this.setType = setType;
        this.designEditable = designEditable;
        this.editHide = editHide;
        this.showType = showType;
        this.sort = sort;
        this.visible = visible;
        this.required = required;
        this.screenType = screenType;
    }

    /**
     * 对data内的字段拼上“data.”
     *
     * @param cache 每个枚举
     * @return java.lang.String
     * @author 徐俊杰
     * @date 2019/3/13 14:44
     * @since v1.0
     */
    public static String getAttrConnectData(PayPlanEnum cache) {
        return "data." + cache.getAttr();
    }

    /**
     * 获取es查询、聚合字段
     * data在库内是json对象，对data字段内某个key做es【精确查询】时，需要在其前拼上“data.”，且【text等类型的】要在后面拼“.keyword”
     * @param cache 每个枚举
     * @return java.lang.String
     * @author 徐俊杰
     * @date 2019/3/13 14:49
     * @since v1.0
     */
    public static String getEsAttr4Keyword(PayPlanEnum cache) {
        FieldTypeEnum fieldTypeEnum = FieldTypeEnum.getFieldTypeEnum(cache.fieldType);
        //字段类型为text，或者字段在es内的类型时text，则需要在后面拼上“.keyword”
        boolean flag = fieldTypeEnum == FieldTypeEnum.TEXT || "text".equals(fieldTypeEnum.getEsalias());
        if (flag) {
            return "data." + cache.getAttr() + ".keyword";
        }
        return "data." + cache.getAttr();
    }

    /**
     * app端详情默认标题显示字段
     *
     * @author GuoJun.HU
     * @since V1.0
     * @return List
     * @date 2019/4/8 19:21
     */
    public static List<Object> getTitleAttrList() {
        return Collections.singletonList(PAY_PLAN_NO);
    }

    /**
     * 详情默认摘要
     *
     * @author long.rao
     * @date 2019-06-27 15:47
     */
    public static List<Object> getSummaryAttrList() {
        return Arrays.asList(LINK_PURCHASE, LINK_SUPPLIER, OWNER_ID, ESTIMATE_TIME);
    }

    private static List<String> disableEditAttr = Arrays.asList(PAY_PLAN_NO.getAttr(),AMOUNT.attr,REAL_AMOUNT.attr,UN_AMOUNT.attr,STATUS.attr,LINK_SUPPLIER.attr,LINK_PURCHASE.attr,REAL_PAY_TIME.attr,BELONGER.attr,PAYMENT_METHOD.attr);

    public static boolean disableEdit(String attr) {
        return disableEditAttr.contains(attr);
    }

    private static List<String> unReceiveDisableEditAttr = new ArrayList<>();

    public static boolean unReceiveDisableEdit(String attr) {
        return unReceiveDisableEditAttr.contains(attr);
    }
    /**
     * 把传入的字段（类型为LINK_BUSINESS_SINGLE）的转为其隐藏字段
     * @param attr 字段名
     * @return java.lang.String
     * @author zcp
     * @date 2019/7/4 15:31
     * @since v1.0
     * @version v1.0
     */
    public static String changeToHide4Link(String attr) {
        if (Objects.equals(attr, LINK_SUPPLIER.attr)) {
            return LINK_SUPPLIER_LINK_TEXT.attr;
        } else if (Objects.equals(attr, LINK_PURCHASE.attr)) {
            return LINK_PURCHASE_LINK_TEXT.attr;
        }
        return attr;
    }

    /**
     * 获取不可编辑的字段
     * @param addUnableEdit
     * @param payPlanStatusEnum
     * @return
     */
    public static List<String> getUnableEditAttrList(boolean addUnableEdit, PayPlanStatusEnum payPlanStatusEnum) {
        if ( !addUnableEdit ){
            return new ArrayList<>();
        }
        switch (payPlanStatusEnum) {
            case RECEIVABLES:
            case PARTIAL_RECEIPTS:
                return Arrays.asList(HAS_RED.attr, PAY_PLAN_NO.attr, LINK_SUPPLIER.attr, LINK_PURCHASE.attr, AMOUNT.attr, STATUS.attr);
            case BAD_DEBT:
            case PARTIAL_BAD_DEBT:
                return Arrays.asList(HAS_RED.attr, PAY_PLAN_NO.attr, LINK_SUPPLIER.attr, LINK_PURCHASE.attr, AMOUNT.attr, STATUS.attr, ESTIMATE_TIME.attr);
            default:
                return Arrays.asList(HAS_RED.attr);
        }
    }

    public static Boolean getUnableEditAttrMap(boolean addUnableEdit, PayPlanStatusEnum payPlanStatusEnum, String attr) {
        Boolean isEdit = true;
        if ( !addUnableEdit ){
            return isEdit;
        }
        Map<String, PayPlanEnum> unableEditAttrMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        switch (payPlanStatusEnum) {
            case RECEIVABLES:
            case PARTIAL_RECEIPTS:
                unableEditAttrMap.put(HAS_RED.attr, HAS_RED);
                unableEditAttrMap.put(PAY_PLAN_NO.attr, PAY_PLAN_NO);
                unableEditAttrMap.put(LINK_SUPPLIER.attr, LINK_SUPPLIER);
                unableEditAttrMap.put(LINK_PURCHASE.attr, LINK_PURCHASE);
                unableEditAttrMap.put(AMOUNT.attr, AMOUNT);
                unableEditAttrMap.put(STATUS.attr, STATUS);
                break;
            case BAD_DEBT:
            case PARTIAL_BAD_DEBT:
                unableEditAttrMap.put(HAS_RED.attr, HAS_RED);
                unableEditAttrMap.put(PAY_PLAN_NO.attr, PAY_PLAN_NO);
                unableEditAttrMap.put(LINK_SUPPLIER.attr, LINK_SUPPLIER);
                unableEditAttrMap.put(LINK_PURCHASE.attr, LINK_PURCHASE);
                unableEditAttrMap.put(AMOUNT.attr, AMOUNT);
                unableEditAttrMap.put(STATUS.attr, STATUS);
                unableEditAttrMap.put(ESTIMATE_TIME.attr, ESTIMATE_TIME);
                break;
            default:
                unableEditAttrMap.put(HAS_RED.attr, HAS_RED);
                break;
        }
        if ( unableEditAttrMap.containsKey(attr) ){
            isEdit = false;
        }
        return isEdit;
    }

    public String getAttrName() {
        return I18nMessageUtil.getMessage(attrName);
    }

    public static String getI18nValue(String value) {
        return I18nMessageUtil.getMessage(value);
    }

    public static List<String> getAttrs() {
        List<String> attrs = new ArrayList<>();
        for (PayPlanEnum payPlanEnum : values()) {
            attrs.add(payPlanEnum.getAttr());
        }
        return attrs;
    }
}
